package cn.com.thingo.hyintelligent.presenter;

import javax.inject.Inject;

import cn.com.thingo.hyintelligent.base.RxPresenter;
import cn.com.thingo.hyintelligent.base.contract.MainContract;
import cn.com.thingo.hyintelligent.model.DataManager;
import cn.com.thingo.hyintelligent.model.bean.BaseBean;
import cn.com.thingo.hyintelligent.model.bean.EzAccountBean;
import cn.com.thingo.hyintelligent.model.bean.GetEzTokenBean;
import cn.com.thingo.hyintelligent.model.bean.UserBean;
import cn.com.thingo.hyintelligent.util.LogUtil;
import cn.com.thingo.hyintelligent.util.RxUtil;
import io.reactivex.functions.Consumer;
import cn.com.thingo.hyintelligent.model.bean.CommonBean;
import cn.com.thingo.hyintelligent.model.bean.MessageCountBean;
import cn.com.thingo.hyintelligent.util.ToastUtil;

public class MainPresenter extends RxPresenter<MainContract.View> implements MainContract.Presenter {

    private DataManager dataManager;

    @Inject
    public MainPresenter(DataManager dataManager) {
        this.dataManager = dataManager;
    }



    @Override
    public void createEzAccount(String token, String md5AccountName, String md5Password,String accessToken) {
        addSubscribe(dataManager.createEzAccount(token, md5AccountName, md5Password,accessToken).compose(RxUtil.<BaseBean>rxSchedulerHelper()).subscribe(new Consumer<BaseBean>() {
            @Override
            public void accept(BaseBean ezAccountBean) throws Exception {
                mView.viewCreateEzAccountResult(ezAccountBean);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                LogUtil.e(throwable.getMessage());
                mView.ezInitError();
            }
        }));
    }



    @Override
    public void getAdminEzAccessToken(String token, String secret, String appkey) {
        addSubscribe(dataManager.getAdminEzToken(token, secret, appkey).compose(RxUtil.<BaseBean<String>>rxSchedulerHelper()).subscribe(new Consumer<BaseBean<String>>() {
            @Override
            public void accept(BaseBean<String> ezTokenBean) throws Exception {
                mView.viewGetAdminEzAccessToken(ezTokenBean);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                LogUtil.e(throwable.getMessage());
                mView.ezInitError();
            }
        }));
    }

    @Override
    public void getSonAccountMsg(String token, String accessToken) {
        addSubscribe(dataManager.getSonAccountMsg(token, accessToken).compose(RxUtil.<BaseBean<EzAccountBean>>rxSchedulerHelper()).subscribe(new Consumer<BaseBean<EzAccountBean>>() {
            @Override
            public void accept(BaseBean<EzAccountBean> bean) throws Exception {
                mView.getSonAccountMsg(bean);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                LogUtil.e(throwable.getMessage());
                mView.ezInitError();
            }
        }));
    }

    @Override
    public void messageCount(String token) {
        addSubscribe(dataManager.messageCount(token)
                .compose(RxUtil.<MessageCountBean>rxSchedulerHelper())
                .subscribe(new Consumer<MessageCountBean>() {
                    @Override
                    public void accept(MessageCountBean messageCountBean) throws Exception {
                        if (messageCountBean.getStatus() == 1) {
                            mView.viewMessageCount(messageCountBean);
                        } else {
                            ToastUtil.shortShow(messageCountBean.getMsg());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        LogUtil.e(throwable.getMessage());
                    }
                }));
    }

    @Override
    public void addMessage(String token, String title, String content) {
        addSubscribe(dataManager.addMessage(token, title, content)
                .compose(RxUtil.<CommonBean>rxSchedulerHelper())
                .subscribe(new Consumer<CommonBean>() {
                    @Override
                    public void accept(CommonBean commonBean) throws Exception {
                        if (commonBean.getStatus() == 1) {
                            mView.viewAddMessage(commonBean);
                        } else {
                            ToastUtil.shortShow(commonBean.getMsg());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        LogUtil.e(throwable.getMessage());
                    }
                }));

    }
}
